<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="88px">
			<el-form-item label="档案盒号" prop="boxNo">
				<el-input v-model="queryParams.boxNo" placeholder="请输入档案盒号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案区编号" prop="archAreaNo">
				<el-tree-select  @change="changeArea" v-model="queryParams.archAreaNo" :data="areaOptions"
					:props="{ value: 'archAreaNo', label: 'label', children: 'children' }" value-key="archAreaNo"
					placeholder="请选择档案区" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案柜" prop="cabinetNo">
				<el-tree-select v-model="queryParams.cabinetNo" :data="cabinetOptions"
					:props="{ value: 'colNo', label: 'label', children: 'children' }" value-key="colNo"
					placeholder="请选择档案柜" clearable @keyup.enter="handleQuery" @click="getListArchiveCabinet"/>
			</el-form-item>
			<el-form-item label="申请编号" prop="orderNo">
				<el-input v-model="queryParams.orderNo" placeholder="请输入申请编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="档案名称" prop="archiveName">
				<el-input v-model="queryParams.archiveName" placeholder="请输入档案名称" clearable
					@keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="借阅标志" prop="borrowStatus">
				<el-select v-model="queryParams.borrowStatus" placeholder="请选择借阅标志" clearable>
					<el-option v-for="dict in borrow_flag" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="新增盘盈件" prop="addStatus">
				<el-select v-model="queryParams.addStatus" placeholder="请选择是否新增盘盈件" clearable>
					<el-option v-for="dict in add_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="盘点结果" prop="inventoryResult">
				<el-select v-model="queryParams.inventoryResult" placeholder="请选择盘点结果" clearable>
					<el-option v-for="dict in inventory_result" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>

			<el-form-item label="制定日期" style="width:400px;">
				<el-date-picker v-model="dateRange" value-format="YYYY-MM-DD" type="daterange" range-separator="-"
					start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport"
					v-hasPermi="['archive:ArchiveStocktaking:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="ArchiveStocktakingList" @selection-change="handleSelectionChange">
			<el-table-column label="申请编号" align="center" prop="orderNo" width="150" />
			<el-table-column label="盘点任务内容" align="center" prop="inventoryContent" width="200" />
			<el-table-column label="盘点制定日期" align="center" prop="inventoryDate" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.inventoryDate) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="盘点人员" align="center" prop="inventoryBy" />
			<el-table-column label="档案柜数量" align="center" prop="cabinetNum" width="100" />
			<el-table-column label="档案盒数量" align="center" prop="boxNum" width="100" />
			<el-table-column label="档案区编号" align="center" prop="archAreaNo" width="100" />
			<el-table-column label="档案柜编号" align="center" prop="colNo" width="100" />
			<el-table-column label="档案盒号" align="center" prop="boxNo" width="150" />
			<el-table-column label="档案名称" align="center" prop="archiveName" width="330" />
			<el-table-column label="档案类型" align="center" prop="archType" width="150">
				<template #default="scope">
					<dict-tag :options="archive_type" :value="scope.row.archType" />
				</template>
			</el-table-column>
			<el-table-column label="档案密级" align="center" prop="confdLv" width="100">
				<template #default="scope">
					<dict-tag :options="confd_lv" :value="scope.row.confdLv" />
				</template>
			</el-table-column>
			<el-table-column label="保管期限" align="center" prop="keepDur" width="100">
				<template #default="scope">
					<dict-tag :options="keep_dur" :value="scope.row.keepDur" />
				</template>
			</el-table-column>
			<el-table-column label="介质类型" align="center" prop="medForm">
				<template #default="scope">
					<dict-tag :options="med_form" :value="scope.row.medForm" />
				</template>
			</el-table-column>
			<el-table-column label="对象类型" align="center" prop="archObjType" width="150">
				<template #default="scope">
					<dict-tag :options="arch_obj_type" :value="scope.row.archObjType" />
				</template>
			</el-table-column>
			<el-table-column label="对象编号" align="center" prop="archObjNo" width="150" />
			<el-table-column label="对象名称" align="center" prop="archObjName" />
			<el-table-column label="新增盘盈件" align="center" prop="addStatus" width="100">
				<template #default="scope">
					<dict-tag :options="add_status" :value="scope.row.addStatus" />
				</template>
			</el-table-column>
			<el-table-column label="盘点结果" align="center" prop="inventoryResult">
				<template #default="scope">
					<dict-tag :options="inventory_result" :value="scope.row.inventoryResult" />
				</template>
			</el-table-column>
			<!-- <el-table-column label="库位号" prop="houseLocationNo" align="center" />
			<el-table-column label="实际库位号" prop="actualHouseLocationNo" align="center" width="100" /> -->
			<el-table-column label="借阅标志" align="center" prop="borrowStatus">
				<template #default="scope">
					<dict-tag :options="borrow_flag" :value="scope.row.borrowStatus" />
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
			v-model:limit="queryParams.pageSize" @pagination="getList" />
	</div>
</template>

<script setup name="ArchiveStocktaking">
	import {
		listArchiveStocktaking,
		getArchiveStocktaking,
		delArchiveStocktaking,
		addArchiveStocktaking,
		updateArchiveStocktaking
	} from "@/api/archive/ArchiveStocktaking";
	import {
		listArchiveCabinetAll
	} from "@/api/archive/ArchiveCabinet";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	const dateRange = ref([]);
	const {
		proxy
	} = getCurrentInstance();
	const {
		inventory_result,
		borrow_flag,
		add_status,
		archive_type,
		save_extended,
		keep_dur,
		confd_lv,
		med_form,
		means_type,
		arch_obj_type,
		oper_type
	} = proxy.useDict('inventory_result', 'borrow_flag', 'add_status', 'archive_type',
		'save_extended', 'keep_dur', 'confd_lv', 'med_form', 'means_type', 'arch_obj_type','oper_type');

	const ArchiveStocktakingList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const areaOptions = ref([]);
	const cabinetOptions = ref([]);
	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			orderNo: null,
			custNo: null,
			custName: null,
			archiveName: null,
			borrowStatus: null,
			addStatus: null,
			inventoryResult: null,
			archAreaNo: null,
			colNo: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
		},
		rules: {}
	});

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);

	/** 查询盘点数据列表 */
	function getList() {
		loading.value = true;
		//listArchiveStocktaking(queryParams.value).then(response => {
		listArchiveStocktaking(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
			ArchiveStocktakingList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			id: null,
			orderNo: null,
			archAreaNo: null,
			custName: null,
			archiveName: null,
			borrowStatus: null,
			addStatus: null,
			inventoryResult: null,
			boxNo: null,
			archiveType: null,
			archiveName: null,
			meansType: null,
			medForm: null,
			archObjType: null,
			archObjNo: null,
			archObjName: null,
		};
		areaOptions.value = [];
		proxy.resetForm("ArchiveStocktakingRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		areaOptions.value = [];
		dateRange.value = [];
		proxy.resetForm("queryRef");
		handleQuery();
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/ArchiveStocktaking/export', {
			...queryParams.value
		}, `盘点详情记录_${new Date().getTime()}.xlsx`)
	}

	//根据档案区查询档案柜
	function changeArea() {
		cabinetOptions.value = [];
		form.value.cabinetNo = null;
		queryParams.value.cabinetNo = null;
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 查询档案柜下拉树结构 */
	function getListArchiveCabinet(queryq) {
		const query = {
			archiveAreaNo: form.value.archAreaNo || queryParams.value.archAreaNo
		}
		cabinetOptions.value = [];
		// 检查查询条件是否为空
		if (!query || !query.archiveAreaNo) {
			proxy.$modal.msgError("请选择档案区编号！");
			return;
		}

		listArchiveCabinetAll(query).then(response => {
			cabinetOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	initAreaOptions();
	getList();
</script>
